/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package icantalk;

/**
 *
 * @author orfrenkel
 */
import icantalk.voip.VoipHandler;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.swing.event.*;
import javax.swing.Timer;
import javax.swing.tree.*;
import javax.swing.text.html.*;

public class ChatDialog extends JFrame implements ActionListener,Observer, ChatConstants
{
	private MainFrame frame;
	private ChatDialog thisframe;
	private Container container;
	private JEditorPane recv;
	private JTextPane type;
	private JButton send;
	private JButton sendFile;
	private JButton smiley;
	private ImageIcon smileyIcon;
	private JButton sad;
	private ImageIcon sadIcon;
	private JButton wink;
	private ImageIcon winkIcon;
	private JButton shy;
	private ImageIcon shyIcon;
	private JButton love;
	private ImageIcon loveIcon;
	private JButton teeth;
	private ImageIcon teethIcon;
	private JButton question;
	private ImageIcon questionIcon;
	private JButton bye;
	private ImageIcon byeIcon;
	private JButton tongue;
	private ImageIcon tongueIcon;
	private JButton surprise;
	private ImageIcon surpriseIcon;
	private JButton angry;
	private ImageIcon angryIcon;
	private JButton mean;
	private ImageIcon meanIcon;
	private JButton cool;
	private ImageIcon coolIcon;
	private JButton devil;
	private ImageIcon devilIcon;
	private JButton cry;
	private ImageIcon cryIcon;
	private JButton laugh;
	private ImageIcon laughIcon;
	private JButton equ;
	private ImageIcon equIcon;
	private JButton coffee;
	private ImageIcon coffeeIcon;	
	private JButton voipCallBtn;
	private JButton voipHangBtn;
	private JButton emoticonBtn;
	private User user;
	private int hwnd;
	private Timer timer=null;
	boolean isFocused = false;
	private int w,h;
	
	final JFileChooser fc = new JFileChooser();
	
	private VoipHandler voip = new VoipHandler();

	public ChatDialog(MainFrame frame,User user)
	{
		this.frame = frame;
		this.user = user;
		initAwtContainer();
	}

	public void initAwtContainer()
	{
		thisframe = this;
		container= this.getContentPane();
		container.setLayout(null);

		recv = new JEditorPane();
		recv.setEditorKit(new HTMLEditorKit());
		//recv.setFont(new Font("Arial",Font.PLAIN,9));
		recv.setEditable(false);

		JScrollPane pane
			= new JScrollPane(recv,
					JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pane.setBounds(60,10,320,100);

		type = new JTextPane();
		type.setFont(new Font("Arial",Font.PLAIN,11));
		

		JScrollPane typepane
			= new JScrollPane(type,
					JScrollPane.VERTICAL_SCROLLBAR_NEVER,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		typepane.setBounds(60,120,220,50);
//Icons:
		smileyIcon = new ImageIcon("src/images/1.gif",":)");
		smiley = new JButton(smileyIcon);
		smiley.setBounds(1,1,20,20);
		smiley.setVisible(false);
		smiley.addActionListener(this);
		
		sadIcon = new ImageIcon("src/images/2.gif");
		sad = new JButton(sadIcon);
		sad.setBounds(41,1, 20, 20);
		sad.setVisible(false);
		sad.addActionListener(this);

		shyIcon = new ImageIcon("src/images/4.gif");
		shy = new JButton(shyIcon);
		shy.setBounds(21,1, 20, 20);
		shy.setVisible(false);
		shy.addActionListener(this);

		winkIcon = new ImageIcon("src/images/3.gif");
		wink = new JButton(winkIcon);
		wink.setBounds(1,21, 20, 20);
		wink.setVisible(false);
		wink.addActionListener(this);

		loveIcon = new ImageIcon("src/images/5.gif");
		love = new JButton(loveIcon);
		love.setBounds(21,21, 20, 20);
		love.setVisible(false);
		love.addActionListener(this);

		teethIcon = new ImageIcon("src/images/6.gif");
		teeth = new JButton(teethIcon);
		teeth.setBounds(41,21, 20, 20);
		teeth.setVisible(false);
		teeth.addActionListener(this);

		questionIcon = new ImageIcon("src/images/7.gif");
		question = new JButton(questionIcon);
		question.setBounds(1,41, 20, 20);
		question.setVisible(false);
		question.addActionListener(this);

		byeIcon = new ImageIcon("src/images/8.gif");
		bye = new JButton(byeIcon);
		bye.setBounds(21,41, 20, 20);
		bye.setVisible(false);
		bye.addActionListener(this);

		tongueIcon = new ImageIcon("src/images/9.gif");
		tongue = new JButton(tongueIcon);
		tongue.setBounds(41,41, 20, 20);
		tongue.setVisible(false);
		tongue.addActionListener(this);


		surpriseIcon = new ImageIcon("src/images/11.gif");
		surprise = new JButton(surpriseIcon);
		surprise.setBounds(1,61, 20, 20);
		surprise.setVisible(false);
		surprise.addActionListener(this);

		angryIcon = new ImageIcon("src/images/12.gif");
		angry = new JButton(angryIcon);
		angry.setBounds(21,61, 20, 20);
		angry.setVisible(false);
		angry.addActionListener(this);

		meanIcon = new ImageIcon("src/images/13.gif");
		mean = new JButton(meanIcon);
		mean.setBounds(41,61, 20, 20);
		mean.setVisible(false);
		mean.addActionListener(this);

		coolIcon = new ImageIcon("src/images/14.gif");
		cool = new JButton(coolIcon);
		cool.setBounds(1,81, 20, 20);
		cool.setVisible(false);
		cool.addActionListener(this);

		devilIcon = new ImageIcon("src/images/16.gif");
		devil = new JButton(devilIcon);
		devil.setBounds(21,81, 20, 20);
		devil.setVisible(false);
		devil.addActionListener(this);
		
		cryIcon = new ImageIcon("src/images/17.gif");
		cry = new JButton(cryIcon);
		cry.setBounds(41,81, 20, 20);
		cry.setVisible(false);
		cry.addActionListener(this);
		
		laughIcon = new ImageIcon("src/images/18.gif");
		laugh = new JButton(laughIcon);
		laugh.setBounds(1,101, 20, 20);
		laugh.setVisible(false);
		laugh.addActionListener(this);

		equIcon = new ImageIcon("src/images/19.gif");
		equ = new JButton(equIcon);
		equ.setBounds(21,101, 20, 20);
		equ.setVisible(false);
		equ.addActionListener(this);

		coffeeIcon = new ImageIcon("src/images/20.gif");
		coffee = new JButton(coffeeIcon);
		coffee.setBounds(41,101, 20, 20);
		coffee.setVisible(false);
		coffee.addActionListener(this);
		//END

		send = new JButton("  Send");
		send.setBounds(290,120,100,40);
		send.addActionListener(this);
		

		voipCallBtn = new JButton("", new ImageIcon("src/images/voip_icon2.png") );
		voipCallBtn.setBounds(5,10,50,50);
		voipCallBtn.addActionListener(this);
		
		voipHangBtn = new JButton("", new ImageIcon("src/images/voip_hang_icon.png") );
		voipHangBtn.setBounds(5,10,50,50);
		voipHangBtn.addActionListener(this);
		voipHangBtn.setVisible(false);

		sendFile = new JButton("",new ImageIcon("src/images/file_icon.png") );
		sendFile.setBounds(5,65,50,50);
		sendFile.addActionListener(this);

		emoticonBtn = new JButton("",new ImageIcon("src/images/1.gif") );
		emoticonBtn.setBounds(5,125,50,50);
		emoticonBtn.addActionListener(this);
		
		container.add(pane);
		container.add(typepane);
		container.add(smiley);
		container.add(shy);
		container.add(sad);
		container.add(wink);
		container.add(love);
		container.add(teeth);
		container.add(question);
		container.add(bye);
		container.add(tongue);
		container.add(mean);
		container.add(cool);
		container.add(devil);
		container.add(surprise);
		container.add(angry);
		container.add(cry);
		container.add(laugh);
		container.add(equ);
		container.add(coffee);
		container.add(send);
		container.add(sendFile);
		container.add(voipCallBtn);
		container.add(voipHangBtn);
		container.add(emoticonBtn);

		type.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke)
			{
				if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
					setVisible(false);
					frame.removeFrame(user);
				} else if(ke.getKeyCode() == KeyEvent.VK_ENTER) {
					if(type.getText().length() == 0) return;
					appendData(frame.getMe().toString(),type.getText(),false);
					Message mymessage = new Message(PRIVATE_CHAT,type.getText());
					mymessage._destination = user.toString();
					try {
						frame.sendMessageToServer(mymessage);
					}
					catch(Exception e) {
						JOptionPane.showMessageDialog(container,
							"Error sending message! Please try again",
							"Error",JOptionPane.ERROR_MESSAGE);
						}
					type.setText("");
				}
			}
		});


		type.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke)
			{
				if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
					setVisible(false);
					frame.removeFrame(user);
				}
			}
		});


		send.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent ke)
					{
						if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
							setVisible(false);
							frame.removeFrame(user);
						}
					}
		});

		recv.addMouseListener(new MouseInputAdapter() {
			public void mouseClicked(MouseEvent me) {
				isFocused = true;
				if(timer != null) timer.stop();
			}
		});

		this.setResizable(false);
		this.setSize(410,210);
		this.setTitle(user+" - Message");
		this.setLocation(300,300);

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e)
			{
				setVisible(false);
				if(timer != null) timer.stop();
				frame.removeFrame(user);
			}

			public void windowActivated(WindowEvent ae) {
				isFocused = true;
				if(timer != null) timer.stop();
			}

			public void windowDeactivated(WindowEvent ae) {
				isFocused = false;
			}
    		public void windowOpened( WindowEvent e ){
    		    type.requestFocus();
    	    }
		});

		this.setVisible(true);
		type.requestFocus();

		//sun.awt.SunToolkit tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit();
		//hwnd = tk.getNativeWindowHandleFromComponent(this);
		timer = new Timer(500,new FlashwindowListener(thisframe));
		isFocused = false;
	}
	public void MakeVisibleBattuns()
	{
		voipCallBtn.setVisible(!(voipCallBtn.isVisible()));
		sendFile.setVisible(!(sendFile.isVisible()));
		smiley.setVisible(!(smiley.isVisible()));
		shy.setVisible(!(shy.isVisible()));
		wink.setVisible(!(wink.isVisible()));
		sad.setVisible(!(sad.isVisible()));
		love.setVisible(!(love.isVisible()));
		teeth.setVisible(!(teeth.isVisible()));
		question.setVisible(!(question.isVisible()));
		bye.setVisible(!(bye.isVisible()));
		tongue.setVisible(!(tongue.isVisible()));
		surprise.setVisible(!(surprise.isVisible()));
		angry.setVisible(!(angry.isVisible()));
		mean.setVisible(!(mean.isVisible()));
		cool.setVisible(!(cool.isVisible()));
		devil.setVisible(!(devil.isVisible()));
		cry.setVisible(!(cry.isVisible()));
		laugh.setVisible(!(laugh.isVisible()));
		equ.setVisible(!(equ.isVisible()));
		coffee.setVisible(!(coffee.isVisible()));
		
	}



	public void startFlashing()
	{
		isFocused = false;
		timer.start();
	}

	public void update(Observable observable,Object object)
	{
		Message message = (Message)object;
		if(!message._username.equals(user.toString()))
			return;
		switch(message._header)	{
			case CLIENT_LOGOUT:
				appendData(null,
					"<FONT COLOR='red' STYLE='font-size:10pt;font-family:Arial'><b>"+user+" logged off at "+(new Date())+"<b></font>",
					false);
				type.setEnabled(false);
				send.setEnabled(false);
			break;
			case PRIVATE_CHAT:
				appendData(user.toString(),message.getMessage(),true);
				if(!isFocused) timer.start();
			break;
		}
	}



	public String toString()
	{
		return user.toString();
	}

	public void appendData(String user,String str,boolean received)
	{
		StringBuffer  bfr= new StringBuffer(str);

		while(str.indexOf("X(") != -1) {
			int index = str.indexOf("X(");
			bfr.replace(index,index+2,"<IMG SRC=\""+ANGRY +"\">");
			str= bfr.toString();
		}
		while(str.indexOf("x(") != -1) {
			int index = str.indexOf("x(");
			bfr.replace(index,index+2,"<IMG SRC=\""+ANGRY +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":))") != -1) {
			int index = str.indexOf(":))");
			bfr.replace(index,index+3,"<IMG SRC=\""+LAUGH +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":((") != -1) {
			int index = str.indexOf(":((");
			bfr.replace(index,index+3,"<IMG SRC=\""+CRY +"\">");
			str= bfr.toString();
		}
		while(str.indexOf("B-)") != -1) {
			int index = str.indexOf("B-)");
			bfr.replace(index,index+3,"<IMG SRC=\""+COOL +"\">");
			str= bfr.toString();
		}
		while(str.indexOf("=;") != -1) {
			int index = str.indexOf("=;");
			bfr.replace(index,index+2,"<IMG SRC=\""+BYE +"\">");
			str= bfr.toString();
		}

		while(str.indexOf(">:)") != -1) {
			int index = str.indexOf(">:)");
			bfr.replace(index,index+3,"<IMG SRC=\""+DEVIL +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":|") != -1) {
			int index = str.indexOf(":|");
			bfr.replace(index,index+2,"<IMG SRC=\""+EQUISMILE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":-|") != -1) {
			int index = str.indexOf(":-|");
			bfr.replace(index,index+3,"<IMG SRC=\""+EQUISMILE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":-P") != -1) {
			int index = str.indexOf(":-P");
			bfr.replace(index,index+3,"<IMG SRC=\""+TONGUE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":P") != -1) {
			int index = str.indexOf(":P");
			bfr.replace(index,index+2,"<IMG SRC=\""+TONGUE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":-o") != -1) {
			int index = str.indexOf(":-o");
			bfr.replace(index,index+3,"<IMG SRC=\""+SURPRISE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":-O") != -1) {
			int index = str.indexOf(":-O");
			bfr.replace(index,index+3,"<IMG SRC=\""+SURPRISE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf("~0)") != -1) {
			int index = str.indexOf("~0)");
			bfr.replace(index,index+3,"<IMG SRC=\""+COFFEE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf("~o)") != -1) {
			int index = str.indexOf("~o)");
			bfr.replace(index,index+3,"<IMG SRC=\""+COFFEE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":>") != -1) {
			int index = str.indexOf(":>");
			bfr.replace(index,index+2,"<IMG SRC=\""+MEAN +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":->") != -1) {
			int index = str.indexOf(":->");
			bfr.replace(index,index+3,"<IMG SRC=\""+MEAN +"\">");
			str= bfr.toString();
		}

		while(str.indexOf(":\">") != -1) {
			int index = str.indexOf(":\">");
			bfr.replace(index,index+3,"<IMG SRC=\""+SHY +"\">");
			str= bfr.toString();
		}

		while(str.indexOf(":-/") != -1) {
			int index = str.indexOf(":-/");
			bfr.replace(index,index+3,"<IMG SRC=\""+QUESTION +"\">");
			str= bfr.toString();
		}

		while(str.indexOf(":O") != -1) {
			int index = str.indexOf(":O");
			bfr.replace(index,index+2,"<IMG SRC=\""+SURPRISE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":o") != -1) {
			int index = str.indexOf(":o");
			bfr.replace(index,index+2,"<IMG SRC=\""+SURPRISE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":(") != -1) {
			int index = str.indexOf(":(");
			bfr.replace(index,index+2,"<IMG SRC=\""+SAD +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":-(") != -1) {
			int index = str.indexOf(":-(");
			bfr.replace(index,index+3,"<IMG SRC=\""+SAD +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":)") != -1) {
			int index = str.indexOf(":)");
			bfr.replace(index,index+2,"<IMG SRC=\""+SMILE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":-)") != -1) {
			int index = str.indexOf(":-)");
			bfr.replace(index,index+3,"<IMG SRC=\""+SMILE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(";)") != -1) {
			int index = str.indexOf(";)");
			bfr.replace(index,index+2,"<IMG SRC=\""+WINK +"\">");
			str= bfr.toString();
		}

		while(str.indexOf(";-)") != -1) {
			int index = str.indexOf(";-)");
			bfr.replace(index,index+3,"<IMG SRC=\""+WINK +"\">");
			str= bfr.toString();
		}

		while(str.indexOf(":x") != -1) {
			int index = str.indexOf(":x");
			bfr.replace(index,index+2,"<IMG SRC=\""+LOVE +"\">");
			str= bfr.toString();
		}
		while(str.indexOf(":-x") != -1) {
			int index = str.indexOf(":-x");
			bfr.replace(index,index+3,"<IMG SRC=\""+LOVE +"\">");
			str= bfr.toString();
		}

		if(user != null) {
			if(received) {
				str ="<FONT COLOR='red' STYLE='font-size:10pt;font-family:Arial'>"+user+": </FONT><FONT STYLE='font-size:10pt;font-family:Arial'>"+str;
			} else {
				str ="<FONT COLOR='blue' STYLE='font-size:10pt;font-family:Arial'>"+user+": </FONT><FONT STYLE='font-size:10pt;font-family:Arial'>"+str;
			}
		} else {
			str ="<FONT COLOR='red' STYLE='font-size:10pt;font-family:Arial'><B>"+str;
		}

		str+="</FONT>";//Line break

		try {
		((HTMLEditorKit)recv.getEditorKit()).read(new java.io.StringReader(str),
		 						recv.getDocument(), recv.getDocument().getLength());
		 recv.setCaretPosition(recv.getDocument().getLength());
	 	} catch(Exception e){}
	}

	public void actionPerformed(ActionEvent event)
	{
		if((event.getSource() == type)||(event.getSource() == send)) {
			if(type.getText().length() == 0) return;
			appendData(frame.getMe().toString(),type.getText(),false);
			Message mymessage = new Message(PRIVATE_CHAT,type.getText());
			//System.out.println(user);
			mymessage._destination = user.toString();
			try {
				frame.sendMessageToServer(mymessage);
			}
			catch(Exception e) {
				System.out.println("Error sending message");
			}
			type.setText("");
		}
		if ( event.getSource() == sendFile )
		{
			String filePath = "";
			
			int returnVal = fc.showOpenDialog(ChatDialog.this);

	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            //This is where a real application would open the file.
	           
	            filePath = file.getAbsolutePath();
	        }
	        else
	        {
	        	return;
	        }
	        
			//if(type.getText().length() == 0) return;
			final Message mymessage = new Message(FILE_TRANSFER);
			mymessage._data = filePath; //type.getText();
			
			mymessage._destination = user.toString();
			
				appendData("SYSTEM", "Started Transfering file to user", false);
				
				new Thread ( new Runnable(){
					
					@Override
					public void run() {
						try {
							frame.sendFileToServer(mymessage);
							appendData("SYSTEM", "File transfer completed", false);
						}
						catch(Exception e) {
							System.out.println("Error sending message");
						}
					}
						
				}).start();
					
		}
		if ( event.getSource() == voipCallBtn || event.getSource() == voipHangBtn )
		{

			
			Message mymessage = new Message(VOIP);
			//mymessage._data = type.getText();
			
			
			mymessage._destination = user.toString();
			try {
				frame.sendVoipToServer(mymessage);
			}
			catch(Exception e) {
				System.out.println("Error sending message");
			}
			type.setText("");

		}
		if ( event.getSource() == smiley )
		{
			String st = type.getText();
			type.setText(st + ":)");
			type.insertIcon(smileyIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == sad )
		{
			String st = type.getText();
			type.setText(st + ":(");
			type.insertIcon(sadIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == wink )
		{
			String st = type.getText();
			type.setText(st + ";-)");
			type.insertIcon(winkIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == shy )
		{
			String st = type.getText();
			type.setText(st + ":\">");
			type.insertIcon(shyIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == love )
		{
			String st = type.getText();
			type.setText(st + ":-x");
			type.insertIcon(loveIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == teeth )
		{
			String st = type.getText();
			type.setText(st + ":()");
			type.insertIcon(teethIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == question )
		{
			String st = type.getText();
			type.setText(st + ":-/");
			type.insertIcon(questionIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == tongue )
		{
			String st = type.getText();
			type.setText(st + ":P");
			type.insertIcon(tongueIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == bye )
		{
			String st = type.getText();
			type.setText(st + "=;");
			type.insertIcon(byeIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == surprise )
		{
			String st = type.getText();
			type.setText(st + ":o");
			type.insertIcon(surpriseIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == angry )
		{
			String st = type.getText();
			type.setText(st + "x(");
			type.insertIcon(angryIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == mean )
		{
			String st = type.getText();
			type.setText(st + ":->");
			type.insertIcon(meanIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == cool )
		{
			String st = type.getText();
			type.setText(st + "B-)");
			type.insertIcon(coolIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == devil )
		{
			String st = type.getText();
			type.setText(st + ">:)");
			type.insertIcon(devilIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == cry )
		{
			String st = type.getText();
			type.setText(st + ":((");
			type.insertIcon(cryIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == laugh )
		{
			String st = type.getText();
			type.setText(st + ":))");
			type.insertIcon(laughIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == equ )
		{
			String st = type.getText();
			type.setText(st + ":|");
			type.insertIcon(equIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		if ( event.getSource() == coffee )
		{
			String st = type.getText();
			type.setText(st + "~0");
			type.insertIcon(coffeeIcon);
			type.requestFocus();
			MakeVisibleBattuns();
		}
		

		if ( event.getSource() == emoticonBtn )
		{
			MakeVisibleBattuns();
		}
	}
	
	public void setCallStatus(boolean callStatus)
	{
		if ( !callStatus )
		{
			voipCallBtn.setVisible(true);
			voipHangBtn.setVisible(false);
			
			appendData("SYSTEM","Voip Disconnected" ,false);
			
			
		}
		else
		{
			voipCallBtn.setVisible(false);
			voipHangBtn.setVisible(true);
			appendData("SYSTEM","Voip Connected. You can talk now!" ,false);
		}
	}
	
	public void setChatText(String message)
	{
		type.setText(message);
	}
	
	public String getChatUserName()
	{
		return user.getUserName();
	}
	
}



//
//	FlashWindowListener: Class for flashing the msg window.
//						 Uses JNI call to achieve that
//


class FlashwindowListener implements ActionListener
{
	private Window chatwindow;
	private final native void flashWindow(Window chatwindow);

	public FlashwindowListener(Window window)
	{
		this.chatwindow = window;
	}

	public void actionPerformed(ActionEvent ae)
	{
		//flashWindow(chatwindow);
	}
}
